<?php

// 店铺表

namespace app\client\model;

use think\Model;
use think\Session;
use app\client\model\ShopGoods as ShopGoodsModel;

class Shop extends Model
{
    protected function getTypeTextAttr($value, $data)
    {
        $type = [1 => '个人店铺', 2 => '公司店铺', 3 => '自营店铺'];
        return $type[$data['type']];
    }

    // 模型事件
    protected static function init()
    {

        // 写入前
        self::event('before_write', function ($data) {

            $data->logo = uploadImg('logo', 'shop');            // LOGO
            $data->banner = uploadImg('banner', 'shop');        // banner
            $data->top_banner = uploadImg('top_banner', 'shop');   // top_banner
            $data->m_banner = uploadImg('m_banner', 'shop');   // top_banner
            $data->update_time =  time();
        });

        // 写入后
        self::event('after_write', function ($data) {
            if (!empty($data->rebate_percentage)) {

                (new ShopGoodsModel)->where(['member_id' => Session::get('shop_id')])->update(['rebate_percentage' => $data->rebate_percentage]);
            }
        });

    }
}